Skip to content

Require coherence of supertrait associated item bounds for dyn-compability - #158915

Open
theemathas wants to merge 2 commits into
rust-lang:mainfrom
theemathas:coherent-dyn-compat
Open

Require coherence of supertrait associated item bounds for dyn-compability#158915
theemathas wants to merge 2 commits into
rust-lang:mainfrom
theemathas:coherent-dyn-compat

Conversation

@theemathas

@theemathas theemathas commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This PR is an alternative to #157710. However, unlike that PR, this PR does not address #150936.

Fixes #154662

In a dyn type, if multiple bounds are specified via supertraits for the same associated item, then we previously accepted them if the relevant trait's generics are different, even if the bounds conflict.

This was unsound, since those generics could end up being instantiated with identical concrete types, causing the dyn type to have two different "values" for the same bound.

Thus, if a trait has multiple supertrait bounds for the same associated item, we check whether those bounds are coherent, similarly to how we check for overlap between impls (i.e., we check if the generics could be instantiated to be the same while the "values" of the bounds are different). If the bounds are incoherent, then we consider the trait to be dyn-incompatible.

r? @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 7, 2026
@theemathas

Copy link
Copy Markdown
Contributor Author

I'm unsure of what to do with the now-failing tests.

I'm also rather unsure of my code in general. Please be careful with the review to make sure I didn't do anything stupid. Thank you.

@theemathas theemathas added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Jul 7, 2026
@theemathas
theemathas force-pushed the coherent-dyn-compat branch from 4d67fa1 to 256670e Compare July 7, 2026 15:07
@theemathas

Copy link
Copy Markdown
Contributor Author

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jul 21, 2026
@rustbot rustbot assigned lcnr and unassigned fmease Jul 21, 2026
Comment on lines +1057 to +1062
// Note that we discard any obligations we get here.
// We don't care about proving them.
//
// If this call returns an Err, then the two sets of generic args
// can't possibly be instantiated with the same concrete types.
// So, we return true from the function

@lcnr lcnr Jul 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could do the proper coherence thing and actually use an ObligationCtxt and try_evaluate

makes slightly more compile

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I don't understand what you mean. What is an ObligationCtxt, and how does using it cause more code to compile?

Comment thread compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs Outdated
) -> bool {
// We syntactically compare the two terms. If they're equal, then
// they do not conflict with each other.
// FIXME: This is an overly strict definition of equality. Could this be done better?

@lcnr lcnr Jul 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we check for equality in the param_env of the trait and prove nested obligations + regions, this should be correct?

as in, what we prove here is

for<trait_params, trait-where-clauses> assoc1 == assoc2
  => assoc1_term == assoc2_term

and we under-approximate by

for<trait_params, trait-where-clauses> assoc1 != assoc2
OR
for<trait_params, trait-where-clauses> assoc1term == assoc2term

which we impl as

!exists<trait_params, trait-where-clauses> assoc1 == assoc2
OR
for<trait_params, trait-where-clauses> assoc1term == assoc2term

so to make this check more permissive, you could check the where-clauses of the trait in the exists check :>

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a nested obligation? What's a nested region? What's "the param_env of the trait"? What does it mean to "check" the where clauses of a trait?

@lcnr

lcnr commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

after that, we should do a crater run 😁

@lcnr

lcnr commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 29, 2026
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rust-bors

This comment has been minimized.

…ics.

See rust-lang#154662

These tests will be fixed in a subsequent commit.
@theemathas
theemathas force-pushed the coherent-dyn-compat branch from 256670e to 4adf718 Compare August 16, 2026 09:25
@rustbot

rustbot commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

…ility

Fixes rust-lang#154662

In a `dyn` type, if multiple bounds are specified via supertraits for
the same associated item, then we previously accepted them if
the relevant trait's generics are different,
even if the bounds conflict.

This was unsound, since those generics could end up being instantiated
with identical concrete types, causing the `dyn` type to have
two different "values" for the same bound.

Thus, if a trait has multiple supertrait bounds for the same
associated item, we check whether those bounds are coherent,
similarly to how we check for overlap between impls (i.e., we check
if the generics could be instantiated to be the same while the "values"
of the bounds are different). If the bounds are incoherent, then we
consider the trait to be dyn-incompatible.
@theemathas
theemathas force-pushed the coherent-dyn-compat branch from 4adf718 to 97592bc Compare August 16, 2026 16:10
@theemathas

theemathas commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

I decided to ask an LLM for assistance with understanding how the trait solver API works, and with double-checking my code for any glaring issues. I still wrote all code manually. In particular, the LLM did the following:

  • Explained the difference between InferCtxt and ObligationCtxt. (The documentation was unfortunately not very helpful to me here. I previously didn't realize that my previous InferCtxt usage generated nested obligations that weren't automatically solved for.)
  • Explained the bug in my initial implementation attempt (which incorrectly called fork_with_typing_mode in an attempt to "invert the polarity" between the two ObligationCtxts).
  • Pointed out the existence of plug_infer_with_placeholders, which I verified to have the behavior that I needed.
  • Pointed out that I needed to call resolve_regions. (I think that this is a rather unfortunate footgun that doesn't seem to be properly documented.)
  • Pointed out a potential problem due to me not instantiating the param_env. I don't understand what this problem is, so I'm deferring to you on what to do here. See the comment below.

I claim full responsibility of all code in this PR.

Comment on lines +1074 to +1082
// Now that we've constrained the two projections to be on the same thing,
// we check whether the two terms are necessarily equal to each other.
// If they are, then the two projections are coherent.
plug_infer_with_placeholders(&infcx, ty::UniverseIndex::ROOT, (proj_1, proj_2));
let ocx = ObligationCtxt::new(&infcx);
ocx.eq(&ObligationCause::dummy(), param_env, proj_1.term, proj_2.term).is_ok()
&& ocx.evaluate_obligations_error_on_ambiguity().no_errors()
&& ocx.resolve_regions(CRATE_DEF_ID, param_env, []).is_empty()
}

@theemathas theemathas Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LLM, in reviewing my code, told me that I have to instantiate the param_env with trait_args here. However, I should not do this instantiation in the can_equate_generics computation. Is the LLM right here? I don't understand at all what this does.

View changes since the review

@theemathas theemathas Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is currently the only pre-existing test that this PR breaks. What should be done about this test?

View changes since the review

@theemathas

Copy link
Copy Markdown
Contributor Author

Is this PR ready for a crater run?

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-crater This change needs a crater run to check for possible breakage in the ecosystem. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conflicting supertrait associated type in dyn causes unsoundness and ICE

4 participants